<template>
  <a-drawer
      :title="title"
      :width="800"
      placement="right"
      :closable="false"
      @close="close"
      :visible="visible"
  >

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="支付交易编号">
          <a-input-number v-decorator="[ 'transactionId', validatorRules.transactionId ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="生成传输给第三方的退款号">
          <a-input placeholder="请输入生成传输给第三方的退款号" v-decorator="['refundCode', validatorRules.refundCode ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="应用编号
     *
     * 不同业务线分配不同的 appId
     * 举个例子，
     * 1. 电商系统的订单，appId = 1024
     * 2. 活动系统的订单，appId = 2048">
          <a-input placeholder="请输入应用编号
     *
     * 不同业务线分配不同的 appId
     * 举个例子，
     * 1. 电商系统的订单，appId = 1024
     * 2. 活动系统的订单，appId = 2048" v-decorator="['appId', validatorRules.appId ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="业务线的订单编号
     *
     * 1. 使用 String 的原因是，业务线可能使用 String 做为编号
     * 2. 每个 appId 下，orderId 唯一">
          <a-input placeholder="请输入业务线的订单编号
     *
     * 1. 使用 String 的原因是，业务线可能使用 String 做为编号
     * 2. 每个 appId 下，orderId 唯一" v-decorator="['orderId', validatorRules.orderId ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="发起交易的 IP">
          <a-input placeholder="请输入发起交易的 IP" v-decorator="['createIp', validatorRules.createIp ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="业务退款描述">
          <a-input placeholder="请输入业务退款描述" v-decorator="['orderDescription', validatorRules.orderDescription ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="退款金额，单位：分。">
          <a-input-number v-decorator="[ 'price', validatorRules.price ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="退款状态
     *
     * @see cn.iocoder.mall.pay.api.constant.PayRefundStatus">
          <a-input placeholder="请输入退款状态
     *
     * @see cn.iocoder.mall.pay.api.constant.PayRefundStatus" v-decorator="['status', validatorRules.status ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="回调业务线完成时间">
          <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'finishTime', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="异步通知地址">
          <a-input placeholder="请输入异步通知地址" v-decorator="['notifyUrl', validatorRules.notifyUrl ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="扩展内容
     *
     * 异步通知的时候填充回调的数据">
          <a-input placeholder="请输入扩展内容
     *
     * 异步通知的时候填充回调的数据" v-decorator="['extensionData', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="退款渠道">
          <a-input-number v-decorator="[ 'refundChannel', validatorRules.refundChannel ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="第三方退款成功的时间">
          <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'refundTime', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="收到第三方系统通知的时间
     *
     * 一般情况下，即第三方系统的异步通知">
          <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'notifyTime', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="第三方的流水号">
          <a-input placeholder="请输入第三方的流水号" v-decorator="['tradeNo', {}]" />
        </a-form-item>
		
      </a-form>
    </a-spin>
    <a-button type="primary" @click="handleOk">确定</a-button>
    <a-button type="primary" @click="handleCancel">取消</a-button>
  </a-drawer>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"

  export default {
    name: "RefundModal",
    data () {
      return {
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        transactionId:{rules: [{ required: true, message: '请输入支付交易编号!' }]},
        refundCode:{rules: [{ required: true, message: '请输入生成传输给第三方的退款号!' }]},
        appId:{rules: [{ required: true, message: '请输入应用编号' }]},
        orderId:{rules: [{ required: true, message: '请输入业务线的订单编号' }]},
        createIp:{rules: [{ required: true, message: '请输入发起交易的 IP!' }]},
        orderDescription:{rules: [{ required: true, message: '请输入业务退款描述!' }]},
        price:{rules: [{ required: true, message: '请输入退款金额，单位：分。!' }]},
        status:{rules: [{ required: true, message: '请输入退款状态' }]},
        notifyUrl:{rules: [{ required: true, message: '请输入异步通知地址!' }]},
        refundChannel:{rules: [{ required: true, message: '请输入退款渠道!' }]},
        },
        url: {
          add: "/refund/refund/add",
          edit: "/refund/refund/edit",
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'transactionId','refundCode','appId','orderId','createIp','orderDescription','price','status','notifyUrl','extensionData','refundChannel','tradeNo'))
		  //时间格式化
          this.form.setFieldsValue({finishTime:this.model.finishTime?moment(this.model.finishTime):null})
          this.form.setFieldsValue({refundTime:this.model.refundTime?moment(this.model.refundTime):null})
          this.form.setFieldsValue({notifyTime:this.model.notifyTime?moment(this.model.notifyTime):null})
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            formData.finishTime = formData.finishTime?formData.finishTime.format('YYYY-MM-DD HH:mm:ss'):null;
            formData.refundTime = formData.refundTime?formData.refundTime.format('YYYY-MM-DD HH:mm:ss'):null;
            formData.notifyTime = formData.notifyTime?formData.notifyTime.format('YYYY-MM-DD HH:mm:ss'):null;
            
            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>
/** Button按钮间距 */
  .ant-btn {
    margin-left: 30px;
    margin-bottom: 30px;
    float: right;
  }
</style>